package com.changshi.video.controller;

import com.changshi.common.pojo.Result;
import com.changshi.common.pojo.StatusCode;
import com.changshi.service.video.api.entity.Video;
import com.changshi.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName VideoController
 * @Date 2020/1/2 20:03
 * @Author LeeSwag
 * @Version 1.0
 **/
@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @PostMapping("/add")
    public Result add(@RequestBody Video video) {
        videoService.add(video);
        return new Result(true, StatusCode.OK, "添加视频成功");
    }

    @GetMapping("/username/{username}")
    public Result<List<Video>> get(@PathVariable String username) {
        List<Video> videoList = videoService.findVideoByUsername(username);
        return new Result<>(true, StatusCode.OK, "获取视频成功", videoList);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        videoService.delete(id);
        return new Result(true, StatusCode.OK, "删除视频成功");
    }

    @PutMapping("/update")
    public Result update(@RequestBody Video video) {
        videoService.update(video);
        return new Result(true, StatusCode.OK, "视频更新成功");
    }

    @GetMapping("/{id}")
    public Result<List<Video>> findById(@PathVariable(value = "id") String id) {
        if ("all".equals(id))
            return new Result<>(true, StatusCode.OK, "查询视频成功", videoService.findAll());
        long videoId = Long.parseLong(id);
        Video video = videoService.findById(videoId);
        int playbackCount = video.getPlaybackAmount() + 1;
        videoService.playbackCountPlus(videoId,playbackCount);
        return new Result<>(true, StatusCode.OK, "查询视频成功");
    }
}
